package service

import (
	"context"
	"fmt"
	"fresh-food/model"
	"fresh-food/request"
	"fresh-food/response"
	"github.com/gin-gonic/gin"
	"github.com/jjl309/oneGroup"
)

func UserLogin(c *gin.Context) {
	var data request.UserLoginReq
	if err := c.ShouldBind(&data); err != nil {
		response.CurrencyErrResponse(c, err.Error(), 101)
		return
	}
	key := fmt.Sprintf("code:%s", data.Tel)
	res, err := oneGroup.Rdb.Get(context.Background(), key).Result()
	if err != nil {
		response.CurrencyErrResponse(c, err.Error(), 101)
		return
	}
	if res != data.Code {
		response.CurrencyErrResponse(c, "验证码错误", -1)
		return
	}
	var user model.User
	err = user.GetUserInfoByUserByTel(data.Tel)
	if err != nil {
		response.CurrencyErrResponse(c, err.Error(), -1)
		return
	}
	if user.ID == 0 {
		response.CurrencyErrResponse(c, "请前往注册", -1)
		return
	}
	response.CurrencySuccessResponse(c, "登录成功", map[string]interface{}{"userid:": user.ID})
}
